package org.JWCB.system.service.sysuser.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.JWCB.common.core.domain.entity.LoginUser;
import org.JWCB.common.core.domain.vo.LoginUserVo;
import org.JWCB.common.core.domain.vo.R;
import org.JWCB.system.domain.sysuser.dto.SysUserSaveDTO;
import org.JWCB.system.domain.sysuser.entity.SysUser;
import org.JWCB.common.core.enums.ResultCode;
import org.JWCB.common.core.enums.UserIdentity;
import org.JWCB.common.core.exception.ServiceException;
import org.JWCB.system.mapper.sysuser.SysUserMapper;
import org.JWCB.common.security.service.TokenService;
import org.JWCB.system.service.sysuser.ISysUserService;
import org.JWCB.system.utils.BCryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@Service
@RefreshScope
public class SysUserServiceImpl implements ISysUserService {
    /**
     * 从nacos读取配置
     */
    @Value("${jwt.secret}")
    private String secret;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Autowired
    private TokenService tokenService;

   @Override
    public R<String> login(String userAccount,String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper.select(SysUser::getPassword,SysUser::getUserId,SysUser::getNickName).eq(SysUser::getUserAccount, userAccount));
        if(sysUser == null) {
            return R.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(!BCryptUtils.matchesPassword(password,sysUser.getPassword())) {
            return R.fail(ResultCode.FAILED_LOGIN);
        }
        // 账号密码匹配成功
        String token = tokenService.createToken(sysUser.getUserId(),sysUser.getNickName(),secret, UserIdentity.ADMIN.getValue());
        return R.ok(token);
    }


    @Override
    public int add(SysUserSaveDTO sysUserSaveDTO) {
        SysUser user = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().select(SysUser::getUserId).eq(SysUser::getUserAccount, sysUserSaveDTO.getUserAccount()));
        if(user != null) {
            // 账号已经存在
            throw new ServiceException(ResultCode.AILED_USER_EXISTS);
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserAccount(sysUserSaveDTO.getUserAccount());
        sysUser.setPassword(BCryptUtils.encryptPassword(sysUserSaveDTO.getPassword()));
        sysUser.setNickName(sysUserSaveDTO.getNickName());
        return sysUserMapper.insert(sysUser);
    }

    @Override
    public R<LoginUserVo> info(String token) {
        LoginUser loginUser =  tokenService.getLoginUser(token,secret);
        if(loginUser == null) {
            return R.fail();
        }
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setNickName(loginUser.getNickName());
        return R.ok(loginUserVo);
    }

    @Override
    public Boolean logout(String token) {
        return tokenService.logout(token,secret);
    }
}
